package com.kylin.artwork.manage.starter.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年05月27日 15:07
 */
@Configuration
@EnableSwagger2
public class MySwaggerConfigure {

    /** 是否开启swagger */
    @Value("${swagger.enabled:false}")
    private boolean enabled;

    @Bean
    public Docket createBigfireApi() {
        List<Parameter> parameters = new ArrayList<>();

        parameters.add(new ParameterBuilder()
                .name("token")
                .description("令牌")
                .required(true)
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .build());

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enabled)
                .apiInfo(createApiInfo())
                .forCodeGeneration(true)
                .select()
                // 扫描所有有注解的api，用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo createApiInfo() {
        return new ApiInfoBuilder()
                .title("标题：麒麟团购_接口文档")
                .description("描述：麒麟团购接口管理...")
                // 作者信息
                .contact(new Contact("ruoyiConfig.getName()", null, null))
                .version("版本号:v1")
                .build();
    }
}
